//index.js
//获取应用实例
const app = getApp();
var userInfo;
const DB = wx.cloud.database()
var that;
Page({
  data: {
    phone_numble:'',
    userType:'',
    items:[
      {value: '我是学生', name: '我是学生'},
      {value: '我是老师', name: '我是老师'}]
  }, 
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      userType:e.detail.value
    })
  },
  phone(e){
    console.log('获取手机号',e.detail.value)
    this.setData({
      phone_numble: e.detail.value
    })
  },
  onLoad() {
    that = this;
    if(wx.getStorageSync('user')){
      wx.switchTab({
        url: '/pages/studycentre/studycentre',
      })
    }
  },
  userAdd(openId) {
    var phone_numble = this.data.phone_numble;
    if (!phone_numble || phone_numble.length!=11){
      wx.showToast({
        title: '手机号填写有误',
        icon:'none'
      })
      return
    }
    if (this.data.userType=="我是老师"){
      console.log('是老师,填写的手机号码是', phone_numble)
      DB.collection('teacher').where({
        phone_numble: phone_numble,
      })
        .get().then(ress => {
          console.log('teacher-----ress.data', ress.data)
          // 判断老师的数据库里面有没有这个手机号 phone_numble
          if(ress.data.length>0){
            var user = {
              userType: this.data.userType,
              nickName: userInfo.nickName,
              avatarUrl: userInfo.avatarUrl,
              openId: openId
            };
            wx.setStorageSync('user', user);

            DB.collection('user').add({
              // data 字段表示需新增的 JSON 数据
              data: user
            }).then(res => {
              console.log(res)
              wx.setStorageSync("db_id", res._id)
              wx.switchTab({
                url: '/pages/studycentre/studycentre',
              })
            })
          } else {
            wx.showToast({
              title: '只有老师才可以登陆老师的身份哦',
              icon: 'none'
            })
            return
          }
        })
    }else{

      var user = {
        userType: this.data.userType,
        nickName: userInfo.nickName,
        avatarUrl: userInfo.avatarUrl,
        openId: openId
      };
      wx.setStorageSync('user', user);

      DB.collection('user').add({
        // data 字段表示需新增的 JSON 数据
        data: user
      }).then(res => {
        console.log(res)
        wx.setStorageSync("db_id", res._id)
        wx.switchTab({
          url: '/pages/studycentre/studycentre',
        })
      })
    }
  },
  getUserInfo: function (e) {
    if(!this.data.userType){
      wx.showToast({
        title: '选择身份后才可登录',
        icon:'none'
      })
      return
    }
    console.log(e)
    userInfo = e.detail.userInfo;

    wx.cloud.callFunction({
      name: "getOpenid",
      success(res) {
        let openId = res.result.userInfo.openId
        console.log('我的openid', openId)
        that.userAdd(openId)
        return
        // 判断数据库中是否已经有数据
        DB.collection('user').where({
          openId: openId,
        })
        .get().then(ress => {
          console.log('ressressressressressressressress',ress.data[0])
          
          if (ress.data.length == 0) {
            that.userAdd(openId)
          } else {
            wx.setStorageSync('user', ress.data[0]);
            wx.switchTab({
              url: '/pages/studycentre/studycentre',
            })
          }
        })
      },
      fail(res) {
        console.log('登录失败', res)
      }
    })

  }
})